package com.ycx.springboot_vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycx.springboot_vue.bean.Furn;
import com.ycx.springboot_vue.service.FurnService;
import com.ycx.springboot_vue.utils.Result;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
public class FurnController {
    @Resource
    private FurnService furnService;
    @Resource
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/list")
    public Result<List<Furn>> furnList() {
        System.out.println("datasource:=>   " + jdbcTemplate.getDataSource().getClass());

        List<Furn> list = furnService.list();
        return Result.success(list);
    }

    @PostMapping("/save")
    public Result save(@RequestBody @Validated Furn furn, Errors errors) {
        Map<String, String> map = new HashMap<>();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        if (map.isEmpty()) {
            furnService.save(furn);
            return Result.success();
        } else {
            return Result.error("400", "后端校验失败", map);
        }

    }

    @PostMapping("/update")
    public Result update(@RequestBody Furn furn) {
        furnService.updateById(furn);
        return Result.success();
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        furnService.removeById(id);
        return Result.success();
    }

    @GetMapping("/selectLike/{str}")
    public Result selectLike(@PathVariable("str") String str) {
//        furnService.g;
        return Result.success();
    }

    @GetMapping("/selectOne/{id}")
    public Result getFurnById(@PathVariable("id") Integer id) {
        Furn furn = furnService.getById(id);
        return Result.success(furn);
    }

    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        Page<Furn> page = furnService.page(new Page<>(pageNo, pageSize));
        return Result.success(page);
    }

    @GetMapping("/fuzzy")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       @RequestParam(defaultValue = "") String search) {
//        QueryWrapper<Furn> queryWrapper = Wrappers.query();
//        if (StringUtils.hasText(search)) {
//            queryWrapper.like("name", search);
//        }
//        Page<Furn> page = furnService.page(new Page<>(pageNo, pageSize), queryWrapper);

        LambdaQueryWrapper<Furn> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasText(search)) {
            lambdaQueryWrapper.like(Furn::getName, search);
        }
        Page<Furn> page = furnService.page(new Page<>(pageNo, pageSize), lambdaQueryWrapper);
        return Result.success(page);


    }
}
